package com.wjf.ks_test.vo;

/**
 * @author 吴建飞
 * @version 1.0
 * @since 2024/11/11
 */
public class Rectangle implements Polygon{
    private Vertex[] vertices;

    public Rectangle(Vertex[] vertices) {
        this.vertices = vertices;
    }

    /**
     * 简单判断，矩形需要四个顶点且相对边相等
     * @param vertices
     * @param otherParams
     * @return 布尔类型
     */
    @Override
    public boolean canForm(Vertex[] vertices, double[] otherParams) {

        if (vertices.length!= 4) {
            return false;
        }
        double[] sideLengths = new double[4];
        sideLengths[0] = vertices[0].calcDistance(vertices[1]);
        sideLengths[1] = vertices[1].calcDistance(vertices[2]);
        sideLengths[2] = vertices[2].calcDistance(vertices[3]);
        sideLengths[3] = vertices[3].calcDistance(vertices[0]);

        return sideLengths[0] == sideLengths[2] && sideLengths[1] == sideLengths[3];
    }

    @Override
    public void draw() {
        System.out.println("绘制矩形");
    }

    /**
     * 计算面积
     * @return 面积
     */
    @Override
    public double calcArea() {
        double length = vertices[0].calcDistance(vertices[1]);
        double width = vertices[1].calcDistance(vertices[2]);
        return length * width;
    }
}
